const formatTime = date => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(
        ':');
};

const formatNumber = n => {
    n = n.toString();
    return n[1] ? n : '0' + n;
};
const getNowdate = date => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    return year + '-' + month + '-' + day;
};

const toBase64 = imagePath => {
    return new Promise((resolve, reject) => {
        uni.getFileSystemManager().readFile({
            filePath: imagePath,
            encoding: 'base64',
            success: (res) => {
                const base64Image = res.data;
                resolve(`data:image/png;base64,${base64Image}`);
            },
            fail: (err) => {
                console.error('读取文件失败:', err);
                reject(err);
            }
        });
    });
};

module.exports = {
    formatTime: formatTime,
    getnowTime: getNowdate,
    toBase64: toBase64
};